package cn.jhc.Adapter;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import cn.jhc.bean.Province;
import cn.jhc.layout.SearchDistrictListView;
import cn.jhc.service.SearchService;

import com.example.shilehui.R;
import com.example.shilehui.SearchCityActivity;

public class SearchDistrictAdapter extends BaseAdapter {

	private ViewHolder _viewHolder;
	private Context _context;
	private List<Province> _list;
	private SearchDistrictListView _listView;
	private int _position;
	private ImageView _imageView;
	
	public SearchDistrictAdapter(Context context,List<Province> list,SearchDistrictListView listView){
		
		_context=context;
		_list=list;
		_listView=listView;
		_listView.setOnItemClickListener(new OnListViewItemClickListener());
		_position=SearchService.getInstance().getDistrictPosition();
		
	}
	
	
	
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return _list.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		
		if(convertView==null){
			
			LayoutInflater layoutInflater=(LayoutInflater)_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView=layoutInflater.inflate(R.layout.search_district_item,null);
			_viewHolder=new ViewHolder();
			_viewHolder.textView=(TextView)convertView.findViewById(R.id.search_district_item_textview);
			_viewHolder.imageView=(ImageView)convertView.findViewById(R.id.search_district_item_imageview);
			
			
			
			convertView.setTag(_viewHolder);
			
			
		}else{
			
			_viewHolder=(ViewHolder)convertView.getTag();
			
		}
		
		_viewHolder.textView.setText(_list.get(position).getProvinceName());
		_viewHolder.textView.setTextColor(Color.BLACK);
		_viewHolder.textView.setFocusable(false);
		
		if(_position==position){
			_viewHolder.imageView.setVisibility(View.VISIBLE);
		}else{
			_viewHolder.imageView.setVisibility(View.GONE);
		}
		
		
		return convertView;
	}
	
	private class ViewHolder{
		
		private TextView textView;
		private ImageView imageView;
		
		
	}

	private class OnListViewItemClickListener implements OnItemClickListener{

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position,
				long arg3) {
			
			
			_imageView=SearchService.getInstance().getAllDistrictImageView();
			_imageView.setVisibility(View.GONE);
			
			SearchService.getInstance().SetCityPosition(99999999);
			SearchService.getInstance().SetDistrictPosition(position);
			_position=SearchService.getInstance().getDistrictPosition();
			notifyDataSetChanged();		
			
			
			SearchService.getInstance().setProvince(_list.get(position).getProvinceName());
			
			try {
				Thread.sleep(200);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if(position<=6){
						
				((Activity) _context).finish();
						
			}else{
			
				int pid=_list.get(position).getId();
				SearchService.getInstance().setPId(String.valueOf(pid));
				Intent intent=new Intent(_context, SearchCityActivity.class);
				_context.startActivity(intent);
				
			}
			
			
		}
		
	}
	


}
